package com.tianxiang.utils;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

public class CheckFileSha1 {

	// 计算文件的 SHA-1 值
	public static  String getFileSha1(File file) {
	    if (!file.isFile()) {
	        return null;
	    }
	    MessageDigest digest = null;
	    FileInputStream in = null;
	    byte buffer[] = new byte[8192];
	    int len;
	    try {
	        digest =MessageDigest.getInstance("SHA-1");
	        in = new FileInputStream(file);
	        while ((len = in.read(buffer)) != -1) {
	            digest.update(buffer, 0, len);
	        }
	        BigInteger bigInt = new BigInteger(1, digest.digest());
	        return bigInt.toString(16);
	    } catch (Exception e) {
	        e.printStackTrace();
	        return null;
	    } finally {
	        try {
	            in.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	}
	public static void main(String[] args) {
		CheckFileSha1 cf=new CheckFileSha1();
		File f1=new File("D:\\毕业设计\\disk\\test\\1.txt");
		String code1=cf.getFileSha1(f1);
		File f2=new File("D:\\毕业设计\\disk\\test\\2.txt");
		String code2=cf.getFileSha1(f2);
		System.out.println("文件一:"+code1);
		System.out.println("文件二:"+code2);
	}
}
